package com.lhf.novel.mail;

import com.lhf.novel.sysuser.security.enums.PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * <p>邮件服务</p>
 *
 * @author zy 刘会发
 * @since 2020/3/11$
 */
@Component
public class MailService {

    @Value("${spring.mail.username}")
    private String from;
    @Resource
    private JavaMailSenderImpl javaMailSender;

    public void sendMail(String to, String title, String content) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setText(content,true);
        helper.setSubject(title);
        helper.setSentDate(new Date());
        javaMailSender.send(mimeMessage);
    }
}
